//
//  TIMMainTabBarController.m
//  Demo_sdk
//
//  Created by Deli Xia on 2018/6/28.
//  Copyright © 2018年 Deli Xia. All rights reserved.
//

#import "TIMMainTabBarController.h"
#import "TIMConversionViewController.h"
#import "TIMContactsViewController.h"
#import "TIMMyselfViewController.h"
#import "TIMLoginViewController.h"
#import "TIMDemoMacro.h"
#import "TIMCommon.h"

@interface TIMMainTabBarController ()

@end

@implementation TIMMainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加所有的子控制器
    [self setupTabBar];
    
    _lastViewController = self.childViewControllers.firstObject;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kickOffNotify:) name:TIM_LOGIN_KICKED_OFF_LINE_BROADCAST object:nil];
}

- (void)setupTabBar {
    
    UIImage *clearImage = [self createImageWithColor:[UIColor clearColor]];
    [self.tabBar setShadowImage:clearImage];
    [self.tabBar setBackgroundImage:clearImage];
    self.tabBar.translucent = NO;
    self.tabBar.barTintColor = UIColor.whiteColor;
    
    // 创建子控制器
    TIMConversionViewController *homeVC = [[TIMConversionViewController alloc] init];
    [self setTabBarItem:homeVC.tabBarItem
                  title:@"会话"
              titleSize:10
          selectedImage:@"tab_home_select"
     selectedTitleColor:RGBColor(64,145,247)
            normalImage:@"tab_home_unselect"
       normalTitleColor:RGBColor(153, 153, 153)];
    
    TIMContactsViewController *chatListVC = [[TIMContactsViewController alloc] init];
    [self setTabBarItem:chatListVC.tabBarItem
                  title:@"通讯录"
              titleSize:10
          selectedImage:@"tab_im_select"
     selectedTitleColor:RGBColor(64,145,247)
            normalImage:@"tab_im_unselect"
       normalTitleColor:RGBColor(153, 153, 153)];
    
    TIMMyselfViewController *mineVC = [[TIMMyselfViewController alloc] init];
    [self setTabBarItem:mineVC.tabBarItem
                  title:@"我的"
              titleSize:10
          selectedImage:@"tab_mine_select"
     selectedTitleColor:RGBColor(64,145,247)
            normalImage:@"tab_mine_unselect"
       normalTitleColor:RGBColor(153, 153, 153)];
    
    UINavigationController *homeNav = [[UINavigationController alloc] initWithRootViewController:homeVC];
    UINavigationController *chatListNav = [[UINavigationController alloc] initWithRootViewController:chatListVC];
    UINavigationController *mineNav = [[UINavigationController alloc] initWithRootViewController:mineVC];
    
    // 把子控制器添加到UITabBarController
    self.viewControllers = @[homeNav, chatListNav, mineNav];
    
}

- (void)setTabBarItem:(UITabBarItem *)tabbarItem
                title:(NSString *)title
            titleSize:(CGFloat)size
        selectedImage:(NSString *)selectedImage
   selectedTitleColor:(UIColor *)selectColor
          normalImage:(NSString *)unselectedImage
     normalTitleColor:(UIColor *)unselectColor
{
    //设置图片
    tabbarItem = [tabbarItem initWithTitle:title image:[[UIImage imageNamed:unselectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    // S未选中字体颜色
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:unselectColor,NSFontAttributeName:[UIFont systemFontOfSize:size weight:UIFontWeightRegular]} forState:UIControlStateNormal];
    
    // 选中字体颜色
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:selectColor,NSFontAttributeName:[UIFont systemFontOfSize:size weight:UIFontWeightRegular]} forState:UIControlStateSelected];
}

- (UIImage *)createImageWithColor:(UIColor*)color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

#pragma mark - Notification

- (void)kickOffNotify:(NSNotification *)notification {
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        TIMLoginViewController *logVC = [TIMLoginViewController new];
        [UIApplication sharedApplication].keyWindow.rootViewController = logVC;
        
    }];
    UIAlertController *alert =  [UIAlertController alertControllerWithTitle:@"你被踢了"
                                                                    message:@"你被踢了！"
                                                             preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:action];
    [self presentViewController:alert animated:NO completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
